﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Buyer
{
    public partial class mfAddOrder : msBsc.Cls.mbPCustom
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}
        private void getDjackeVehicle()
        {
            string vehicleId = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataSet ds = vehicleInfo.SelectByIds("", vehicleId);
                if (ds == null||ds.Tables[0].Rows.Count<1)
                {
                    return;
                }
                DataTable dt = new DataTable();
                DataColumn dc1 = new DataColumn("DJACK_ID");
                DataColumn dc2 = new DataColumn("CUSTOMER_NICK");
                DataColumn dc3 = new DataColumn("CUSTOMER_DEFAULT_PIC");
                DataColumn dc4 = new DataColumn("CUSTOMER_BASEINFO_MOBILE");
                DataColumn dc5 = new DataColumn("VEHICLE_INFO_ID");
                DataColumn dc6 = new DataColumn("VEHICLE_NAME");
                DataColumn dc7 = new DataColumn("VEHICLE_SERVICE_TYPE");
                DataColumn dc8 = new DataColumn("VEHICLE_DEFAULT_PIC");
                DataColumn dc9 = new DataColumn("VEHICLE_SEATS");
                DataColumn dc10 = new DataColumn("VEHICLE_PRICE_DAY");
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
                dt.Columns.Add(dc5);
                dt.Columns.Add(dc6);
                dt.Columns.Add(dc7);
                dt.Columns.Add(dc8);
                dt.Columns.Add(dc9);
                dt.Columns.Add(dc10);
                DataRow dr = dt.NewRow();
                dr["DJACK_ID"] = ds.Tables[0].Rows[0]["DJACK_ID"];
                dr["CUSTOMER_NICK"] = ds.Tables[0].Rows[0]["CUSTOMER_NICK"];
                dr["CUSTOMER_DEFAULT_PIC"] = ds.Tables[0].Rows[0]["CUSTOMER_DEFAULT_PIC"];
                dr["CUSTOMER_BASEINFO_MOBILE"] = ds.Tables[0].Rows[0]["CUSTOMER_BASEINFO_MOBILE"];
                dr["VEHICLE_INFO_ID"] = ds.Tables[0].Rows[0]["VEHICLE_INFO_ID"];
                dr["VEHICLE_NAME"] = ds.Tables[0].Rows[0]["VEHICLE_NAME"];
                dr["VEHICLE_SERVICE_TYPE"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.DjackServiceTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[0]["VEHICLE_SERVICE_TYPE"].ToString()));
                dr["VEHICLE_DEFAULT_PIC"] = ds.Tables[0].Rows[0]["VEHICLE_DEFAULT_PIC"];
                dr["VEHICLE_SEATS"] = ds.Tables[0].Rows[0]["VEHICLE_SEATS"];
                dr["VEHICLE_PRICE_DAY"] = ds.Tables[0].Rows[0]["VEHICLE_PRICE_DAY"];
                dt.Rows.Add(dr);
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"init(" + strJson + ")");
            }
        }

        private void getDjackVehicle()
        {
            string djackId = this.GetParamHtValue("djackId");
            if (string.IsNullOrEmpty(djackId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataSet ds = vehicleInfo.GetVehicleForSubmitOrder(djackId);
                if (ds != null && ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                DataTable dt = new DataTable();
                DataColumn dc1 = new DataColumn("VEHICLE_INFO_ID");
                DataColumn dc2 = new DataColumn("VEHICLE_NAME");
                DataColumn dc3 = new DataColumn("VEHICLE_SERVICE_TYPE");
                DataColumn dc4 = new DataColumn("VEHICLE_DEFAULT_PIC");
                DataColumn dc5 = new DataColumn("VEHICLE_SEATS");
                DataColumn dc6 = new DataColumn("VEHICLE_PRICE_DAY");
                dt.Columns.Add(dc1);
                dt.Columns.Add(dc2);
                dt.Columns.Add(dc3);
                dt.Columns.Add(dc4);
                dt.Columns.Add(dc5);
                dt.Columns.Add(dc6);
                foreach (DataRow dr1 in ds.Tables[0].Rows)
                {
                    DataRow dr = dt.NewRow();
                    dr["VEHICLE_INFO_ID"] = dr1["VEHICLE_INFO_ID"];
                    dr["VEHICLE_NAME"] = dr1["VEHICLE_NAME"];
                    dr["VEHICLE_SERVICE_TYPE"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.DjackServiceTypeEnum), Convert.ToInt32(dr1["VEHICLE_SERVICE_TYPE"].ToString()));
                    dr["VEHICLE_DEFAULT_PIC"] = dr1["VEHICLE_DEFAULT_PIC"];
                    dr["VEHICLE_SEATS"] = dr1["VEHICLE_SEATS"];
                    dr["VEHICLE_PRICE_DAY"] = dr1["VEHICLE_PRICE_DAY"];
                    dt.Rows.Add(dr);
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"showVehicle(" + retJson + ")");
            }
        }

        private void setOrders()
        {
            string v_OIds = this.GetParamHtValue("oIds");
            if (string.IsNullOrEmpty(v_OIds))
            {
                this.Alert("请选择车辆");
                this.ExecJScript(@"$(""#submitOrder"").text(""提交订单"");");
                return;
            }
            string v_startTime = this.GetParamHtValue("startTime");
            if (string.IsNullOrEmpty(v_startTime))
            {
                this.Alert("请选择开始服务时间");
                this.ExecJScript(@"$(""#submitOrder"").text(""提交订单"");");
                return;
            }
            string v_stopTime = this.GetParamHtValue("stopTime");
            if (string.IsNullOrEmpty(v_stopTime))
            {
                this.Alert("请选择结束服务时间");
                this.ExecJScript(@"$(""#submitOrder"").text(""提交订单"");");
                return;
            }
            //string v_service = this.GetParamHtValue("service");
            try
            {
                using (msBsc.AppLogic.DataAlc.CBizOrdersInfo ordersInfo = new msBsc.AppLogic.DataAlc.CBizOrdersInfo())
                {
                    string[] strRet = ordersInfo.AddOrderInfoN(v_OIds, v_startTime, v_stopTime, "");
                    if (strRet[0] != "true")
                    {
                        this.Alert(strRet[1]);
                        return;
                    }
                    this.ExecJScript(@"alert(""提交订单成功，请尽快联系地接客，以便确认服务！"");$(""#submitOrder"").text(""提交订单"");window.location.href = "" / "";");
                }
            }
            catch (Exception e)
            {
                this.Alert(e.Message);
                this.ExecJScript(@"$(""#submitOrder"").text(""提交订单"");");
                return;
            }
           
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getDjackeVehicle();
            }
            else if (this.strAction == "getVehicle")
            {
                getDjackVehicle();
            }
            else if (this.strAction == "setOrder")
            {
                setOrders();
            }
        }
	}
}